package com.sls.api.game.servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.api.util.IpUtil;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

public class NutsNetServlet extends SuperServlet {
	private static Logger log = Logger.getLogger(NutsNetServlet.class);
	private static final long serialVersionUID = 6880856609526821313L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		resp.setStatus(HttpServletResponse.SC_OK);
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setIp(IpUtil.getIpAddr(req));
		head.setServiceCode("020002");
		head.setUserId("0");
		try {
			SocketDTO socketChangeVo = super.send(head);
			Object body = socketChangeVo.getBody();
			if (body != null)
				resp.getWriter().write(
						gson.fromJson(body.toString(), String.class));
		} catch (Exception e) {
			log.error("出错", e);
		}
		resp.getWriter().write(req.getLocalAddr());

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
